package com.fitso.web.service;

import org.apache.log4j.Logger;

import com.fitso.model.bean.User;
import com.fitso.model.service.user.UserService;

/**
 * 
 * @author timothystorm
 */
public class UserDWRService extends DWRService {
	private static final String SESSION_USER = "SESSION_USER";
	private static final Logger logger = Logger.getLogger(UserDWRService.class);
	private UserService _service;

	public void setUserService(UserService service) {
		_service = service;
	}

	public User login(String email, String password) {
		logger.info(String.format("login email/pass: %s/%s ", email, password));
		
		User user = getSessionUser();
		if(user == null){
			user = _service.fetch(email, password);
		}
		return user;
	}

	public void register(User user) {
		logger.info(String.format("new user: %s", user));
		_service.add(user);
	}
	
	public User getSessionUser(){
		User user = (User) getSession().getAttribute(SESSION_USER);
		return user;
	}
}
